<template>
  <BasicContainer>
    <div class="avue-crud">
      <ElForm :inline="true">
        <ElFormItem>
          <ElButton
            v-if="permissions.cti_fyassetcti_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)">
            添加
          </ElButton>
          <ElButton
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-upload"
            @click="$refs.excelUpload.show()">
            导入
          </ElButton>
          <ElButton
            v-if="permissions.cti_fyassetcti_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">
            导出
          </ElButton>
        </ElFormItem>
      </ElForm>

      <ElTable v-loading="loading" border :data="ctiList" row-key="id" :tree-props="{ children: 'children' }">
        <ElTableColumn prop="name" label="分类名称" :show-overflow-tooltip="true" />
        <ElTableColumn prop="code" label="分类编码" align="center" />
        <ElTableColumn prop="assetType" label="资产分类" align="center" />
        <ElTableColumn prop="accountingMethod" label="核算分类" align="center" />
        <ElTableColumn prop="createTime" label="创建时间" align="center" />
        <ElTableColumn label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <ElButton
              v-if="permissions.cti_fyassetcti_add"
              size="small"
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false, scope.row.id)">
              新增
            </ElButton>
            <ElButton
              v-if="permissions.cti_fyassetcti_edit"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true, scope.row.id)">
              修改
            </ElButton>
            <ElButton
              v-if="permissions.cti_fyassetcti_del"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">
              删除
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>

      <!--excel 模板导入 -->
      <ExcelUpload
        ref="excelUpload"
        title="资产分类导入"
        :url="`${assetService}fyassetcti/import`"
        :temp-url="`${assetService}fyassetcti/local/ctiTemp.xlsx`"
        temp-name="资产分类导入模板.xlsx"
        @refreshDataList="getList" />

      <CtiForm v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
    </div>
  </BasicContainer>
</template>

<script>
  import CtiForm from './cti-form'
  import { delObj, fetchTree } from '@/api/asset/fyassetcti'
  import { mapState } from 'vuex'

  import { services } from '@/config'

  export default {
    name: 'Cti',
    components: { CtiForm },
    data() {
      return {
        assetService: services.assetService,
        addOrUpdateVisible: false,
        // 遮罩层
        loading: true,
        // 表格树数据
        ctiList: []
      }
    },
    computed: {
      ...mapState('user', ['permissions'])
    },
    created() {
      this.getList()
    },
    methods: {
      addOrUpdateHandle(isEdit, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(isEdit, id)
        })
      },
      getList() {
        this.loading = true
        fetchTree().then(response => {
          this.ctiList = response.data.data
          this.loading = false
        })
      },
      handleDelete(row) {
        this.$confirm(`是否确认删除名称为"${row.name}"的数据项?`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(() => {
            this.getList()
            this.$message.success('删除成功')
          })
      },
      //  导出excel
      exportExcel() {
        this.$downBlobFile(`${services.assetService}fyassetcti/export`, this.searchForm, '资产分类导入信息.xlsx')
      }
    }
  }
</script>
